package com.example.tibetanmedicinechest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.tibetanmedicinechest.mapper.FavoriteMapper;
import com.example.tibetanmedicinechest.model.Case;
import com.example.tibetanmedicinechest.mapper.CaseMapper;
import com.example.tibetanmedicinechest.model.Favorite;
import com.example.tibetanmedicinechest.service.ICaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 编程笨蛋
 * @since 2023-08-12
 */
@Service
public class CaseServiceImpl extends ServiceImpl<CaseMapper, Case> implements ICaseService {
    @Autowired
    CaseMapper caseMapper;
    @Autowired
    FavoriteMapper favoriteMapper;
    @Override
    public List<Map<String,Object>> selectAll() {
        List<Case> cases = caseMapper.selectList(null);
        List<Map<String,Object>> datas = new ArrayList<>();
        for (Case a : cases) {
            Map<String,Object> data = new HashMap<>();
            data.put("images",a.getImage());
            data.put("name",a.getName());
            data.put("introduce",a.getIntroduce());
            data.put("measure",a.getMeasure());
            data.put("id",a.getId());

            LambdaQueryWrapper<Favorite> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Favorite::getUser_id,1).eq(Favorite::getCase_id,a.getId());
            List<Favorite> favorites = favoriteMapper.selectList(lqw);
            System.out.println(favorites);
            System.out.println("*******************************************************************");
            data.put("fav", favorites.size() != 0);

            datas.add(data);
        }

        return datas;
    }

}
